package com.easy.user.controller;

import com.easy.common.bean.User;
import com.easy.common.service.IRegisterService;
import com.easy.common.util.CaptchaUtil;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//处理用户注册
@RestController
@RequestMapping("/user/register")
@CrossOrigin
public class RegisterController {

    @Autowired
    IRegisterService registerService;
    // 注册
    @PostMapping
    public Result register(@RequestBody User user,
                           @RequestParam("captcha") String captcha,
                           @RequestParam("key") String key) {
        // 验证码验证
        if (!CaptchaUtil.checkCaptcha(key, captcha)) {
            return Result.fail(500, "验证码错误");
        }

        try {
            User registeredUser = registerService.register(user);
            return Result.success(registeredUser);
        } catch (Exception e) {
            return Result.fail(500, e.getMessage());
        }
    }
}